package cn.edu.scau.cmi.ema.service;

import cn.edu.scau.cmi.ema.base.CmiPagination;
import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.service.base.CmiEmaService;
import cn.edu.scau.cmi.ema.util.CmiSqlUtil;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.Query;
import java.util.*;
@Service("FoodService")
@Transactional

public class FoodServiceImpl extends CmiEmaService implements FoodService {
	public FoodServiceImpl() {
	}

	@Transactional
	public Food saveFood(Food food) {
		Set<Detectstandard> existingDetectstandardsForFood = new HashSet<Detectstandard>();
		Set<Packingmethod> existingPackingmethodsForPackingmethod = new HashSet<Packingmethod>();
		Set<Processmethod> existingProcessmethodsForProcessmethod = new HashSet<Processmethod>();
		Set<Foodadditive> existingFoodadditivesForFood = new HashSet<Foodadditive>();
		Set<Foodbatch> existingFoodbatchsForFood = new HashSet<Foodbatch>();
		Set<Foodmaterial> existingFoodmaterialsForFood = new HashSet<Foodmaterial>();
		Set<Foodsaleaddress> existingFoodsaleaddresssForFood = new HashSet<Foodsaleaddress>();
		Set<Picture> existingPicturesForFood = new HashSet<Picture>();
		Set<Video> existingVideosForFood = new HashSet<Video>();

		Set<Detectstandard> selectedDetectstandardsForFood = food.getDetectstandardsForFood();
		Set<Packingmethod> selectedPackingmethodsForPackingmethod = food.getPackingmethodsForFood();
		Set<Processmethod> selectedProcessmethodsForProcessmethod = food.getProcessmethodsForFood();
		Set<Foodadditive> selectedFoodadditivesForFood = food.getFoodadditivesForFood();
		Set<Foodbatch> selectedFoodbatchsForFood = food.getFoodbatchsForFood();
		Set<Foodmaterial> selectedFoodmaterialsForFood = food.getFoodmaterialsForFood();
		Set<Foodsaleaddress> selectedFoodsaleaddresssForFood = food.getFoodsaleaddresssForFood();
		Set<Picture> selectedPicturesForFood = food.getPicturesForFood();
		Set<Video> selectedVideosForFood = food.getVideosForFood();

		if (food.getId() == null) {
			Brand brand = brandDAO.findBrandById(food.getBrand().getId());
			Expire expire = expireDAO.findExpireById(food.getExpire().getId());
			Storemethod storemethod = storemethodDAO.findStoremethodById(food.getStoremethod().getId());
			Producer producer = producerDAO.findProducerById(food.getProducer().getId());
			Foodtype foodtype = foodtypeDAO.findFoodtypeById(food.getFoodtype().getId());
			Productcode productcode = productcodeDAO.findProductcodeById(food.getProductcode().getId());
			Taste taste = tasteDAO.findTasteById(food.getTaste().getId());

			Food existingFood = foodDAO.findFoodByPhysicalPrimaryKey(food.getName(), foodtype.getId(),
					productcode.getId(), brand.getId());

			food.setBrand(brand);
			food.setExpire(expire);
			food.setStoremethod(storemethod);
			food.setProducer(producer);
			food.setFoodtype(foodtype);
			food.setProductcode(productcode);
			food.setTaste(taste);

			if (isEquals(food, existingFood)) {
				return existingFood;
			}

			food = foodDAO.store(food);
			foodDAO.flush();

			for (Detectstandard relatedDetectstandard : selectedDetectstandardsForFood) {
				relatedDetectstandard.setFood(food);
				detectstandardDAO.store(relatedDetectstandard);
			}

			for (Packingmethod relatedPackingmethod : selectedPackingmethodsForPackingmethod) {
				relatedPackingmethod.getFoodsForPackingmethod().add(food);
				packingmethodDAO.store(relatedPackingmethod);
			}

			for (Processmethod relatedProcessmethod : selectedProcessmethodsForProcessmethod) {
				relatedProcessmethod.getFoodsForProcessmethod().add(food);
				processmethodDAO.store(relatedProcessmethod);
			}
			for (Foodadditive relatedFoodadditive : selectedFoodadditivesForFood) {
				relatedFoodadditive.setFood(food);
				foodadditiveDAO.store(relatedFoodadditive);
			}
			for (Foodbatch relatedFoodbatch : selectedFoodbatchsForFood) {
				relatedFoodbatch.setFood(food);
				foodbatchDAO.store(relatedFoodbatch);
			}
			for (Foodmaterial relatedFoodmaterial : selectedFoodmaterialsForFood) {
				relatedFoodmaterial.setFood(food);
				foodmaterialDAO.store(relatedFoodmaterial);
			}
			for (Foodsaleaddress relatedFoodsaleaddress : selectedFoodsaleaddresssForFood) {
				relatedFoodsaleaddress.setFood(food);
				foodsaleaddressDAO.store(relatedFoodsaleaddress);
			}
			for (Picture relatedPicture : selectedPicturesForFood) {
				relatedPicture.setFood(food);
				pictureDAO.store(relatedPicture);
			}
			for (Video relatedVideo : selectedVideosForFood) {
				relatedVideo.setFood(food);
				videoDAO.store(relatedVideo);
			}
		} else {
			Food existingFood = foodDAO.findFoodByPrimaryKey(food.getId());

			existingDetectstandardsForFood = existingFood.getDetectstandardsForFood();
			existingPackingmethodsForPackingmethod = existingFood.getPackingmethodsForFood();
			existingProcessmethodsForProcessmethod = existingFood.getProcessmethodsForFood();
			existingFoodadditivesForFood = existingFood.getFoodadditivesForFood();
			existingFoodbatchsForFood = existingFood.getFoodbatchsForFood();
			existingFoodmaterialsForFood = existingFood.getFoodmaterialsForFood();
			existingFoodsaleaddresssForFood = existingFood.getFoodsaleaddresssForFood();
			existingPicturesForFood = existingFood.getPicturesForFood();
			existingVideosForFood = existingFood.getVideosForFood();

			Set<Detectstandard> prepareDeleteDetectstandardsForFood = new HashSet<Detectstandard>();
			Set<Detectstandard> prepareAddDetectstandardsForFood = new HashSet<Detectstandard>();

			prepareDeleteDetectstandardsForFood.addAll(selectedDetectstandardsForFood);
			prepareDeleteDetectstandardsForFood.addAll(existingDetectstandardsForFood);

			prepareAddDetectstandardsForFood.addAll(selectedDetectstandardsForFood);
			prepareAddDetectstandardsForFood.addAll(existingDetectstandardsForFood);

			prepareDeleteDetectstandardsForFood.removeAll(selectedDetectstandardsForFood);
			prepareAddDetectstandardsForFood.removeAll(existingDetectstandardsForFood);

			for (Detectstandard relatedDetectstandard : prepareAddDetectstandardsForFood) {
				relatedDetectstandard.setFood(food);
				detectstandardDAO.store(relatedDetectstandard);
			}

			for (Detectstandard relatedDetectstandard : prepareDeleteDetectstandardsForFood) {
				relatedDetectstandard.setFood(null);
				detectstandardDAO.store(relatedDetectstandard);
			}
			Set<Packingmethod> prepareDeletePackingmethodsForPackingmethod = new HashSet<Packingmethod>();
			Set<Packingmethod> prepareAddPackingmethodsForPackingmethod = new HashSet<Packingmethod>();

			prepareDeletePackingmethodsForPackingmethod.addAll(selectedPackingmethodsForPackingmethod);
			prepareDeletePackingmethodsForPackingmethod.addAll(existingPackingmethodsForPackingmethod);

			prepareAddPackingmethodsForPackingmethod.addAll(selectedPackingmethodsForPackingmethod);
			prepareAddPackingmethodsForPackingmethod.addAll(existingPackingmethodsForPackingmethod);

			prepareDeletePackingmethodsForPackingmethod.removeAll(selectedPackingmethodsForPackingmethod);
			prepareAddPackingmethodsForPackingmethod.removeAll(existingPackingmethodsForPackingmethod);

			for (Packingmethod relatedPackingmethod : prepareAddPackingmethodsForPackingmethod) {

				relatedPackingmethod.getFoodsForPackingmethod().add(existingFood);
				packingmethodDAO.store(relatedPackingmethod);
			}

			for (Packingmethod relatedPackingmethod : prepareDeletePackingmethodsForPackingmethod) {

				relatedPackingmethod.getFoodsForPackingmethod().remove(existingFood);
				packingmethodDAO.store(relatedPackingmethod);
			}
			Set<Processmethod> prepareDeleteProcessmethodsForProcessmethod = new HashSet<Processmethod>();
			Set<Processmethod> prepareAddProcessmethodsForProcessmethod = new HashSet<Processmethod>();

			prepareDeleteProcessmethodsForProcessmethod.addAll(selectedProcessmethodsForProcessmethod);
			prepareDeleteProcessmethodsForProcessmethod.addAll(existingProcessmethodsForProcessmethod);

			prepareAddProcessmethodsForProcessmethod.addAll(selectedProcessmethodsForProcessmethod);
			prepareAddProcessmethodsForProcessmethod.addAll(existingProcessmethodsForProcessmethod);

			prepareDeleteProcessmethodsForProcessmethod.removeAll(selectedProcessmethodsForProcessmethod);
			prepareAddProcessmethodsForProcessmethod.removeAll(existingProcessmethodsForProcessmethod);

			for (Processmethod relatedProcessmethod : prepareAddProcessmethodsForProcessmethod) {

				relatedProcessmethod.getFoodsForProcessmethod().add(existingFood);
				processmethodDAO.store(relatedProcessmethod);
			}

			for (Processmethod relatedProcessmethod : prepareDeleteProcessmethodsForProcessmethod) {

				relatedProcessmethod.getFoodsForProcessmethod().remove(existingFood);
				processmethodDAO.store(relatedProcessmethod);
			}
			Set<Foodadditive> prepareDeleteFoodadditivesForFood = new HashSet<Foodadditive>();
			Set<Foodadditive> prepareAddFoodadditivesForFood = new HashSet<Foodadditive>();

			prepareDeleteFoodadditivesForFood.addAll(selectedFoodadditivesForFood);
			prepareDeleteFoodadditivesForFood.addAll(existingFoodadditivesForFood);

			prepareAddFoodadditivesForFood.addAll(selectedFoodadditivesForFood);
			prepareAddFoodadditivesForFood.addAll(existingFoodadditivesForFood);

			prepareDeleteFoodadditivesForFood.removeAll(selectedFoodadditivesForFood);
			prepareAddFoodadditivesForFood.removeAll(existingFoodadditivesForFood);

			for (Foodadditive relatedFoodadditive : prepareAddFoodadditivesForFood) {
				relatedFoodadditive.setFood(food);
				foodadditiveDAO.store(relatedFoodadditive);
			}

			for (Foodadditive relatedFoodadditive : prepareDeleteFoodadditivesForFood) {
				relatedFoodadditive.setFood(null);
				foodadditiveDAO.store(relatedFoodadditive);
			}
			Set<Foodbatch> prepareDeleteFoodbatchsForFood = new HashSet<Foodbatch>();
			Set<Foodbatch> prepareAddFoodbatchsForFood = new HashSet<Foodbatch>();

			prepareDeleteFoodbatchsForFood.addAll(selectedFoodbatchsForFood);
			prepareDeleteFoodbatchsForFood.addAll(existingFoodbatchsForFood);

			prepareAddFoodbatchsForFood.addAll(selectedFoodbatchsForFood);
			prepareAddFoodbatchsForFood.addAll(existingFoodbatchsForFood);

			prepareDeleteFoodbatchsForFood.removeAll(selectedFoodbatchsForFood);
			prepareAddFoodbatchsForFood.removeAll(existingFoodbatchsForFood);

			for (Foodbatch relatedFoodbatch : prepareAddFoodbatchsForFood) {
				relatedFoodbatch.setFood(food);
				foodbatchDAO.store(relatedFoodbatch);
			}

			for (Foodbatch relatedFoodbatch : prepareDeleteFoodbatchsForFood) {
				relatedFoodbatch.setFood(null);
				foodbatchDAO.store(relatedFoodbatch);
			}
			Set<Foodmaterial> prepareDeleteFoodmaterialsForFood = new HashSet<Foodmaterial>();
			Set<Foodmaterial> prepareAddFoodmaterialsForFood = new HashSet<Foodmaterial>();

			prepareDeleteFoodmaterialsForFood.addAll(selectedFoodmaterialsForFood);
			prepareDeleteFoodmaterialsForFood.addAll(existingFoodmaterialsForFood);

			prepareAddFoodmaterialsForFood.addAll(selectedFoodmaterialsForFood);
			prepareAddFoodmaterialsForFood.addAll(existingFoodmaterialsForFood);

			prepareDeleteFoodmaterialsForFood.removeAll(selectedFoodmaterialsForFood);
			prepareAddFoodmaterialsForFood.removeAll(existingFoodmaterialsForFood);

			for (Foodmaterial relatedFoodmaterial : prepareAddFoodmaterialsForFood) {
				relatedFoodmaterial.setFood(food);
				foodmaterialDAO.store(relatedFoodmaterial);
			}

			for (Foodmaterial relatedFoodmaterial : prepareDeleteFoodmaterialsForFood) {
				relatedFoodmaterial.setFood(null);
				foodmaterialDAO.store(relatedFoodmaterial);
			}
			Set<Foodsaleaddress> prepareDeleteFoodsaleaddresssForFood = new HashSet<Foodsaleaddress>();
			Set<Foodsaleaddress> prepareAddFoodsaleaddresssForFood = new HashSet<Foodsaleaddress>();

			prepareDeleteFoodsaleaddresssForFood.addAll(selectedFoodsaleaddresssForFood);
			prepareDeleteFoodsaleaddresssForFood.addAll(existingFoodsaleaddresssForFood);

			prepareAddFoodsaleaddresssForFood.addAll(selectedFoodsaleaddresssForFood);
			prepareAddFoodsaleaddresssForFood.addAll(existingFoodsaleaddresssForFood);

			prepareDeleteFoodsaleaddresssForFood.removeAll(selectedFoodsaleaddresssForFood);
			prepareAddFoodsaleaddresssForFood.removeAll(existingFoodsaleaddresssForFood);

			for (Foodsaleaddress relatedFoodsaleaddress : prepareAddFoodsaleaddresssForFood) {
				relatedFoodsaleaddress.setFood(food);
				foodsaleaddressDAO.store(relatedFoodsaleaddress);
			}

			for (Foodsaleaddress relatedFoodsaleaddress : prepareDeleteFoodsaleaddresssForFood) {
				relatedFoodsaleaddress.setFood(null);
				foodsaleaddressDAO.store(relatedFoodsaleaddress);
			}
			Set<Picture> prepareDeletePicturesForFood = new HashSet<Picture>();
			Set<Picture> prepareAddPicturesForFood = new HashSet<Picture>();

			prepareDeletePicturesForFood.addAll(selectedPicturesForFood);
			prepareDeletePicturesForFood.addAll(existingPicturesForFood);

			prepareAddPicturesForFood.addAll(selectedPicturesForFood);
			prepareAddPicturesForFood.addAll(existingPicturesForFood);

			prepareDeletePicturesForFood.removeAll(selectedPicturesForFood);
			prepareAddPicturesForFood.removeAll(existingPicturesForFood);

			for (Picture relatedPicture : prepareAddPicturesForFood) {
				relatedPicture.setFood(food);
				pictureDAO.store(relatedPicture);
			}

			for (Picture relatedPicture : prepareDeletePicturesForFood) {
				relatedPicture.setFood(null);
				pictureDAO.store(relatedPicture);
			}
			Set<Video> prepareDeleteVideosForFood = new HashSet<Video>();
			Set<Video> prepareAddVideosForFood = new HashSet<Video>();

			prepareDeleteVideosForFood.addAll(selectedVideosForFood);
			prepareDeleteVideosForFood.addAll(existingVideosForFood);

			prepareAddVideosForFood.addAll(selectedVideosForFood);
			prepareAddVideosForFood.addAll(existingVideosForFood);

			prepareDeleteVideosForFood.removeAll(selectedVideosForFood);
			prepareAddVideosForFood.removeAll(existingVideosForFood);

			for (Video relatedVideo : prepareAddVideosForFood) {
				relatedVideo.setFood(food);
				videoDAO.store(relatedVideo);
			}

			for (Video relatedVideo : prepareDeleteVideosForFood) {
				relatedVideo.setFood(null);
				videoDAO.store(relatedVideo);
			}
			food = foodDAO.store(food);
			foodDAO.flush();
		}

		detectstandardDAO.flush();
		packingmethodDAO.flush();
		processmethodDAO.flush();
		foodadditiveDAO.flush();
		foodbatchDAO.flush();
		foodmaterialDAO.flush();
		foodsaleaddressDAO.flush();
		pictureDAO.flush();
		videoDAO.flush();
		return food;
	}

	public void deleteFood(Food food) {
		foodDAO.remove(food);
		foodDAO.flush();
	}

	@Transactional
	public void deleteFoods(Set<Integer> deleteFoodsId) {
		Query query = foodDAO.createNamedQuery("deleteFoodsById", -1, -1);
		query.setParameter(1, deleteFoodsId);
		query.executeUpdate();
	}

	@Transactional
	public Set<Food> loadFoods() {
		return foodDAO.findAllFoods();

	}

	@Transactional
	public Set<Food> loadReferenceFoods(Set<Food> foods) {
		Iterator<Food> foodsIterator = foods.iterator();
		Set<Food> referencedFoods = new HashSet<>();
		while (foodsIterator.hasNext()) {
			Food currentFood = foodsIterator.next();

			if (!currentFood.getDetectstandardsForFood().isEmpty()) {
				referencedFoods.add(currentFood);
				continue;
			}

			if (!currentFood.getPackingmethodsForFood().isEmpty()) {
				referencedFoods.add(currentFood);
				continue;
			}

			if (!currentFood.getProcessmethodsForFood().isEmpty()) {
				referencedFoods.add(currentFood);
				continue;
			}

			if (!currentFood.getFoodadditivesForFood().isEmpty()) {
				referencedFoods.add(currentFood);
				continue;
			}

			if (!currentFood.getFoodbatchsForFood().isEmpty()) {
				referencedFoods.add(currentFood);
				continue;
			}

			if (!currentFood.getFoodmaterialsForFood().isEmpty()) {
				referencedFoods.add(currentFood);
				continue;
			}

			if (!currentFood.getFoodsaleaddresssForFood().isEmpty()) {
				referencedFoods.add(currentFood);
				continue;
			}

			if (!currentFood.getPicturesForFood().isEmpty()) {
				referencedFoods.add(currentFood);
				continue;
			}

			if (!currentFood.getVideosForFood().isEmpty()) {
				referencedFoods.add(currentFood);
				continue;
			}
		}
		return referencedFoods;
	}

	public Set<Food> loadFoodsByIdSet(Set<Integer> foodIdSet) {

		Iterator<Integer> foodIdIterator = foodIdSet.iterator();
		Set<Food> foodSet = new HashSet<>();
		while (foodIdIterator.hasNext()) {
			Integer id = foodIdIterator.next();
			Food currentFood = foodDAO.findFoodById(id);
			foodSet.add(currentFood);
		}
		return foodSet;
	}

	@Transactional
	public List<Food> findAllFoods(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<Food>(foodDAO.findAllFoods(startResult, maxRows));
	}

	@Transactional
	public Food findFoodByPrimaryKey(Integer id) {
		return foodDAO.findFoodByPrimaryKey(id);
	}

	@Transactional
	public Integer countFoods() {
		return ((Long) foodDAO.createQuerySingleResult("select count(o) from Food o").getSingleResult()).intValue();
	}

	@Transactional
	public Integer countFoods(CmiPagination page) {
		String sql = CmiSqlUtil.createCountHql(page, Food.class);
		return ((Long) foodDAO.createQuerySingleResult(sql).getSingleResult()).intValue();
	}

	/**
	 * search for name with ajax
	 *
	 */
	@Transactional
	public Integer countFoodsByName(String queryName) {
		Query query = foodDAO.createNamedQuery("countFoodsByName", -1, -1);
		query.setParameter(1, queryName);
		return new Long((long) query.getSingleResult()).intValue();
	}

	@Transactional
	public Integer countFoodsByDetachedCriteria(CmiPagination page) {
		DetachedCriteria detachedCriteria = CmiSqlUtil.createDetachedCriteria(page, Food.class);
		Session session = (Session) entityManager.getDelegate();
		detachedCriteria.setProjection(Projections.rowCount());
		return new Long((long) detachedCriteria.getExecutableCriteria(session).uniqueResult()).intValue();
	}

	public Map<String, ?> loadFoodObjectReferencedSet(Food food) {
		HashMap<String, Object> objectReferencedSetMap = new HashMap<>();
		if (food.getBrand() != null) {

			Set<Integer> brandIds = new HashSet<Integer>();
			brandIds.add(food.getBrand().getId());
			objectReferencedSetMap.put("brandIds", brandIds);
		}
		if (food.getExpire() != null) {

			Set<Integer> expireIds = new HashSet<Integer>();
			expireIds.add(food.getExpire().getId());
			objectReferencedSetMap.put("expireIds", expireIds);
		}
		if (food.getStoremethod() != null) {

			Set<Integer> storemethodIds = new HashSet<Integer>();
			storemethodIds.add(food.getStoremethod().getId());
			objectReferencedSetMap.put("storemethodIds", storemethodIds);
		}
		if (food.getProducer() != null) {

			Set<Integer> producerIds = new HashSet<Integer>();
			producerIds.add(food.getProducer().getId());
			objectReferencedSetMap.put("producerIds", producerIds);
		}
		if (food.getFoodtype() != null) {

			Set<Integer> foodtypeIds = new HashSet<Integer>();
			foodtypeIds.add(food.getFoodtype().getId());
			objectReferencedSetMap.put("foodtypeIds", foodtypeIds);
		}
		if (food.getProductcode() != null) {

			Set<Integer> productcodeIds = new HashSet<Integer>();
			productcodeIds.add(food.getProductcode().getId());
			objectReferencedSetMap.put("productcodeIds", productcodeIds);
		}
		if (food.getTaste() != null) {

			Set<Integer> tasteIds = new HashSet<Integer>();
			tasteIds.add(food.getTaste().getId());
			objectReferencedSetMap.put("tasteIds", tasteIds);
		}

		Set<Integer> detectstandardsIds = new HashSet<Integer>();
		for (Detectstandard obj : food.getDetectstandardsForFood()) {

			detectstandardsIds.add(obj.getId());
		}
		objectReferencedSetMap.put("detectstandardsIds", detectstandardsIds);
		Set<Integer> packingmethodsIds = new HashSet<Integer>();
		for (Packingmethod packingmethod : food.getPackingmethodsForFood()) {
			packingmethodsIds.add(packingmethod.getId());
		}
		objectReferencedSetMap.put("packingmethodsIds", packingmethodsIds);
		Set<Integer> processmethodsIds = new HashSet<Integer>();
		for (Processmethod processmethod : food.getProcessmethodsForFood()) {
			processmethodsIds.add(processmethod.getId());
		}
		objectReferencedSetMap.put("processmethodsIds", processmethodsIds);
		Set<Integer> foodadditivesIds = new HashSet<Integer>();
		for (Foodadditive obj : food.getFoodadditivesForFood()) {

			foodadditivesIds.add(obj.getId());
		}
		objectReferencedSetMap.put("foodadditivesIds", foodadditivesIds);
		Set<Integer> foodbatchsIds = new HashSet<Integer>();
		for (Foodbatch obj : food.getFoodbatchsForFood()) {

			foodbatchsIds.add(obj.getId());
		}
		objectReferencedSetMap.put("foodbatchsIds", foodbatchsIds);
		Set<Integer> foodmaterialsIds = new HashSet<Integer>();
		for (Foodmaterial obj : food.getFoodmaterialsForFood()) {

			foodmaterialsIds.add(obj.getId());
		}
		objectReferencedSetMap.put("foodmaterialsIds", foodmaterialsIds);
		Set<Integer> foodsaleaddresssIds = new HashSet<Integer>();
		for (Foodsaleaddress obj : food.getFoodsaleaddresssForFood()) {

			foodsaleaddresssIds.add(obj.getId());
		}
		objectReferencedSetMap.put("foodsaleaddresssIds", foodsaleaddresssIds);
		Set<Integer> picturesIds = new HashSet<Integer>();
		for (Picture obj : food.getPicturesForFood()) {

			picturesIds.add(obj.getId());
		}
		objectReferencedSetMap.put("picturesIds", picturesIds);
		Set<Integer> videosIds = new HashSet<Integer>();
		for (Video obj : food.getVideosForFood()) {

			videosIds.add(obj.getId());
		}
		objectReferencedSetMap.put("videosIds", videosIds);

		return objectReferencedSetMap;
	}

	public Map<String, ?> loadFoodClassReferencedSet(Food food) {
		HashMap<String, Object> classReferencedSetMap = new HashMap<>();
		classReferencedSetMap.put("brands", brandDAO.findAllBrands());
		classReferencedSetMap.put("expires", expireDAO.findAllExpires());
		classReferencedSetMap.put("storemethods", storemethodDAO.findAllStoremethods());
		classReferencedSetMap.put("producers", producerDAO.findAllProducers());
		classReferencedSetMap.put("foodtypes", foodtypeDAO.findAllFoodtypes());
		classReferencedSetMap.put("productcodes", productcodeDAO.findAllProductcodes());
		classReferencedSetMap.put("tastes", tasteDAO.findAllTastes());
		classReferencedSetMap.put("detectstandards", detectstandardDAO.findAllDetectstandards());
		classReferencedSetMap.put("packingmethods", packingmethodDAO.findAllPackingmethods());
		classReferencedSetMap.put("processmethods", processmethodDAO.findAllProcessmethods());
		classReferencedSetMap.put("foodadditives", foodadditiveDAO.findAllFoodadditives());
		classReferencedSetMap.put("foodbatchs", foodbatchDAO.findAllFoodbatchs());
		classReferencedSetMap.put("foodmaterials", foodmaterialDAO.findAllFoodmaterials());
		classReferencedSetMap.put("foodsaleaddresss", foodsaleaddressDAO.findAllFoodsaleaddresss());
		classReferencedSetMap.put("pictures", pictureDAO.findAllPictures());
		classReferencedSetMap.put("videos", videoDAO.findAllVideos());

		return classReferencedSetMap;
	}

	public boolean isEquals(Food food, Object obj) {
		if (obj == food)
			return true;
		if (!(obj instanceof Food))
			return false;
		Food comparedFood = (Food) obj;
		if (!CheckPropertyEqual(food.getName(), comparedFood.getName())) {
			return false;
		}
		if (!CheckPropertyEqual(food.getFoodtype().getId(), comparedFood.getFoodtype().getId())) {
			return false;
		}
		if (!CheckPropertyEqual(food.getProductcode().getId(), comparedFood.getProductcode().getId())) {
			return false;
		}
		if (!CheckPropertyEqual(food.getBrand().getId(), comparedFood.getBrand().getId())) {
			return false;
		}
		return true;
	}

	public static <T> boolean CheckPropertyEqual(T originProperty, T comparedProperty) {
		// 基本类型经过参数传递后会自动装箱
		String[] className = originProperty.getClass().getName().split("\\.");
		if (className[0].equals("java") && className[1].equals("math")) {
			return ((Comparable) originProperty).compareTo(comparedProperty) == 0 ? true : false;
		} else {
			return originProperty.equals(comparedProperty);
		}
	}
}

